#!/bin/bash

# 检查是否提供了两个参数
if [ "$#" -ne 2 ]; then
    echo "Usage: $0 <path_to_c_project> <path_to_rust_project>"
    exit 1
fi

# 读取参数
C_PROJECT_PATH=$1
RUST_PROJECT_PATH=$2

# 确保提供的路径存在
if [ ! -d "$C_PROJECT_PATH" ]; then
    echo "Error: C project path does not exist."
    exit 1
fi

# 清空 Rust 项目目录（如果存在）
rm -rf "$RUST_PROJECT_PATH"
mkdir -p "$RUST_PROJECT_PATH"

rm -rf temp
mkdir temp

# 递归查找包含 main 函数的文件
find_main_files() {
    local dir=$1
    local main_files=()
    for file in "$dir"/*; do
        if [ -d "$file" ]; then
            # 递归调用函数，并将结果添加到 main_files 数组中
            main_files+=($(find_main_files "$file"))
        elif [ -f "$file" ]; then
            # 使用 case 语句来检查文件扩展名是否为 .c 或 .cpp
            case "$file" in (*.c|*.cpp)
                    if grep -qE "int\s+main\s*\(" "$file"; then
                        main_files+=("$file")
                    fi
            esac
        fi
    done
    echo "${main_files[@]}"
}

# 记录每个文件夹下包含 main 函数的文件
main_files=($(find_main_files "$C_PROJECT_PATH"))

# 检查是否有 main 函数
if [ ${#main_files[@]} -eq 0 ]; then
    echo "Error: No main function found in the project."
    exit 1
fi

# 递归查找 .h 文件
find_h_files() {
    local dir=$1
    local h_files=()
    for file in "$dir"/*; do
        if [ -d "$file" ]; then
            h_files+=($(find_h_files "$file"))
        elif [ -f "$file" ]; then
            case "$file" in
            *.h)
                h_files+=("$file")
                ;;
            esac
        fi
    done
    echo "${h_files[@]}"
}

# 记录每个文件夹下 .h 后缀文件
h_files=($(find_h_files "$C_PROJECT_PATH"))

h_dirs=($(for item in "${h_files[@]}"; do
  dirname "$item"
done | uniq))

# 递归查找所有文件
find_all_c_files() {
    local dir=$1
    local all_c_files=()
    for file in "$dir"/*; do
        if [ -d "$file" ]; then
            # 递归调用函数，并将结果添加到 all_c_files 数组中
            all_c_files+=($(find_all_c_files "$file"))
        elif [ -f "$file" ]; then
            # 使用 case 语句来检查文件扩展名是否为 .c 或 .cpp
            case "$file" in (*.c|*.cpp)
                all_c_files+=("$file")
            esac
        fi
    done
    echo "${all_c_files[@]}"
}

# 记录每个文件夹下 .c 或 .cpp后缀文件
all_c_files=($(find_all_c_files "$C_PROJECT_PATH"))

not_main_files=(`echo ${all_c_files[@]} ${main_files[@]} | sed 's/ /\n/g' | sort | uniq -u`)


# 构建 -I 参数
include_args=""
for sub_dir in ${h_dirs[@]}; do
    include_args+=" -I $sub_dir"
done

# 构建 gcc 编译参数，排除当前 main 文件
gcc_args=""
for file in ${not_main_files[@]}; do
    gcc_args+=" $file"
done


# 对每个 main 函数进行转换
for main_file in "${main_files[@]}"; do

#    read -p "gcc -o temp/output ${include_args} ${gcc_args} ${main_file}" _
#    read -p "${main_file}" _

    # 使用 intercept-build 拦截 gcc 编译过程，生成 compile_commands.json
    intercept-build sh -c "gcc -o temp/output ${include_args} ${gcc_args} ${main_file}"

#   read -p "intercept-build sh -c \"gcc -o temp/output ${include_args} ${gcc_args} ${main_file}\"" _

    # 使用 c2rust 转换项目
    c2rust transpile compile_commands.json -e -o "$RUST_PROJECT_PATH"
#   cp -r temp/rust/* "$RUST_PROJECT_PATH"

    # 清理 compile_commands.json
    rm compile_commands.json
done

python generate_lib_rs.py "$RUST_PROJECT_PATH"
rm -rf temp
